package com.sdu.cloudservice.dao;

import com.sdu.cloudservice.entity.InstanceInfoEntity;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Create by zekdot on 19-6-22.
 */
@Mapper
@Repository
public interface InstanceInfoDao {
    /**
     * 插入实例信息到数据库
     * @param id 实例节点id
     * @param value 实例值
     * @param level 实例层级
     * @param parentId 父节点id
     * @return
     */
    @Insert("INSERT INTO instance_info VALUES(#{id},#{value},#{level},#{parentId})")
    public int insertInstance(@Param("id")String id,@Param("value")String value,@Param("level")int level,@Param("parentId") String parentId);

    /**
     * 根据id删除实例
     * @param id 实例id
     * @return
     */
    @Delete("DELETE FROM instance_info WHERE id=#{id}")
    public int deleteInstance(@Param("id")String id);

    /**
     * 获取区域列表
     * @return 包含区域信息的List
     */
    @Select("SELECT * FROM instance_info WHERE level=0")
    public List<InstanceInfoEntity> getAllRegion();

    /**
     * 根据区域id获取可用区列表
     * @param regionId 区域id
     * @return
     */
    @Select("SELECT * FROM instance_info WHERE parent_id=#{id}")
    public List<InstanceInfoEntity> getZoneByRegionId(@Param("id")String regionId);

    /**
     * 根据可用区id获取实例列表
     * @param zoneId 可用区id
     * @return
     */
    @Select("SELECT * FROM instance_info WHERE parent_id=#{id}")
    public List<InstanceInfoEntity> getInstanceByZoneId(@Param("id")String zoneId);

    /**
     * 根据level获取instanceinfo列表
     *@param level
     */
    @Select("SELECT * FROM instance_info WHERE level=#{level}")
    public List<InstanceInfoEntity> getInstanceByLevel(@Param("level")int level);

    /**
     * 根据id更改level
     */
    @Update("UPDATE instance_info SET level=#{level} WHERE id=#{id}")
    public int updateLevelById(@Param("level") int level,@Param("id")String id);
}
